import { create } from 'zustand';

const initialMembers = [
  {
    id: '1',
    slug: 'zhangsan-001',
    nickname: '张三',
    phone: '13800138001',
    birthday: '1990-05-15',
    level: 'gold',
    points: 1280,
    balance: 158.50,
    wechat: 'zhangsan_wx',
    createdAt: '2024-01-10 10:00:00',
    updatedAt: '2024-03-15 14:30:00'
  },
  {
    id: '2',
    slug: 'lisi-002',
    nickname: '李四',
    phone: '13800138002',
    birthday: '1992-08-20',
    level: 'silver',
    points: 680,
    balance: 88.00,
    wechat: 'lisi_2024',
    createdAt: '2024-01-15 11:00:00',
    updatedAt: '2024-03-10 16:20:00'
  },
  {
    id: '3',
    slug: 'wangwu-003',
    nickname: '王五',
    phone: '13800138003',
    birthday: '1988-12-03',
    level: 'platinum',
    points: 2850,
    balance: 388.00,
    wechat: 'wangwu888',
    createdAt: '2023-11-20 09:30:00',
    updatedAt: '2024-03-18 10:15:00'
  },
  {
    id: '4',
    slug: 'zhaoliu-004',
    nickname: '赵六',
    phone: '13800138004',
    birthday: '1995-03-25',
    level: 'bronze',
    points: 230,
    balance: 25.50,
    wechat: 'zhaoliu_95',
    createdAt: '2024-02-01 14:00:00',
    updatedAt: '2024-03-05 12:00:00'
  },
  {
    id: '5',
    slug: 'qianqi-005',
    nickname: '钱七',
    phone: '13800138005',
    birthday: '1993-07-18',
    level: 'gold',
    points: 1520,
    balance: 198.00,
    wechat: 'qianqi_vip',
    createdAt: '2024-01-05 08:00:00',
    updatedAt: '2024-03-12 15:45:00'
  },
  {
    id: '6',
    slug: 'sunba-006',
    nickname: '孙八',
    phone: '13800138006',
    birthday: '1991-11-30',
    level: 'silver',
    points: 450,
    balance: 56.00,
    wechat: 'sunba_wx',
    createdAt: '2024-02-10 10:30:00',
    updatedAt: '2024-03-08 09:20:00'
  }
];

const useMembersStore = create((set, get) => ({
  allMembers: initialMembers,
  members: initialMembers,
  loading: false,
  error: null,
  filters: {
    level: 'all'
  },

  getMembers: async (filters = {}) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      const { allMembers } = get();
      let filteredMembers = [...allMembers];
      
      if (filters.level && filters.level !== 'all') {
        filteredMembers = filteredMembers.filter(
          member => member.level === filters.level
        );
      }

      if (filters.phone) {
        filteredMembers = filteredMembers.filter(
          member => member.phone.includes(filters.phone)
        );
      }
      
      set({ 
        members: filteredMembers, 
        loading: false,
        filters 
      });
    } catch (error) {
      set({ error: error.message, loading: false });
    }
  },

  getMemberById: (id) => {
    const { allMembers } = useMembersStore.getState();
    return allMembers.find(member => member.id === id);
  },

  updateMember: async (id, updates) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set((state) => {
        const updatedAllMembers = state.allMembers.map(member =>
          member.id === id
            ? { ...member, ...updates, updatedAt: new Date().toISOString() }
            : member
        );
        
        return {
          allMembers: updatedAllMembers,
          members: updatedAllMembers,
          loading: false
        };
      });
      
      const { filters } = get();
      await get().getMembers(filters);
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  recharge: async (id, amount) => {
    set({ loading: true });
    try {
      await new Promise(resolve => setTimeout(resolve, 500));
      
      set((state) => {
        const updatedAllMembers = state.allMembers.map(member =>
          member.id === id
            ? { 
                ...member, 
                balance: member.balance + amount,
                updatedAt: new Date().toISOString() 
              }
            : member
        );
        
        return {
          allMembers: updatedAllMembers,
          members: updatedAllMembers,
          loading: false
        };
      });
      
      const { filters } = get();
      await get().getMembers(filters);
    } catch (error) {
      set({ error: error.message, loading: false });
      throw error;
    }
  },

  setFilters: (filters) => {
    set({ filters });
  },

  clearError: () => set({ error: null })
}));

export default useMembersStore;
